package cn.itcast.web.controller;


import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import cn.itcast.utils.Encrypt;
import com.alibaba.excel.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController extends BaseController {


    @Autowired
    private UserService userService;

	@RequestMapping("/login")
	public String login(String email,String password) {

	    if (StringUtils.isEmpty(email)||StringUtils.isEmpty(password)){
            request.setAttribute("error", "账号密码不能为空哦");
	        return "forward:/login.jsp";

        }
	    User user = userService.findByEmail(email);

        password = Encrypt.md5(password,email);

        if (user!=null&&user.getPassword().equals(password)){

            session.setAttribute("loginUser",user);
            return "home/main";
        }else {
            request.setAttribute("error", "邮箱或者密码错误");
            return "forward:/login.jsp";
        }
	}

    //退出
    @RequestMapping(value = "/logout",name="用户登出")
    public String logout(){
        //SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home(){
	    return "home/home";
    }
}
